package com.banzhi.shop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.adapter.CommonHeadAdapter;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.entity.Recommend.Recommend_Data;
import com.banzhi.shop.entity.Recommend.recommendInfo_data;
import com.banzhi.shop.model.CallBack.DZCustomCallBack;
import com.banzhi.shop.model.IDZCustomModel;
import com.banzhi.shop.model.Impl.DZCustomImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.EndlessRecyclerOnScrollListener;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 店主客户的业绩和下线页面
 */
public class DianzhuCustomActivity extends BaseActivity implements View.OnClickListener, OnRefreshListener, DZCustomCallBack {
    private ImageView ivback;
    /**
     * 传过来的店主名字
     */
    private String name;
    /**
     * 传过来的店主ID
     */
    private String id;
    /**
     * 传过来的客户数量
     */
    private String num;
    /**
     * 店主名字
     */
    private TextView tvname;
    /**
     * 客户数量
     */
    private TextView tvnum;
    /**
     * 销售订单
     */
    private TextView tv_order;

    private SwipeRefreshLayout srllistview;
    private RecyclerView recyclerView;
    private IDZCustomModel model;
    private int page = 1;
    private List<recommendInfo_data> data;
    private CommonHeadAdapter<recommendInfo_data> adapter;
    private RelativeLayout rlnodata;
    private boolean loading;
    private LinearLayoutManager layoutManager;
    private String mNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dianzhu_custom);
        Translucent.setTranslucent(this);
        setinits();
        setListeners();
        model = new DZCustomImpl();

        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        id = intent.getStringExtra("id");
        num = intent.getStringExtra("num");
        showViews();
        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
        model.getData(page, id, this);
    }

    private void showViews() {
        tvname.setText(name);
        tvnum.setText(num);

    }

    private void setinits() {
        ivback = (ImageView) findViewById(R.id.iv_back);
        tvname = (TextView) findViewById(R.id.tv_name);
        tvnum = (TextView) findViewById(R.id.tv_num);
        tv_order = (TextView) findViewById(R.id.tv_order);
        srllistview = (SwipeRefreshLayout) findViewById(R.id.srl_listview);
        srllistview.setColorSchemeResources(R.color.themeColor);
        srllistview.setProgressBackgroundColorSchemeResource(R.color.colorWhite);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        data = new ArrayList<>();
        rlnodata = (RelativeLayout) findViewById(R.id.rl_nodata);
    }

    private void setListeners() {
        ivback.setOnClickListener(this);
        srllistview.setOnRefreshListener(this);
        tv_order.setOnClickListener(this);
//        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//                if (dy > 0) //向下滚动
//                {
//                    int visibleItemCount = layoutManager.getChildCount();
//                    int totalItemCount = layoutManager.getItemCount();
//                    int pastVisiblesItems = layoutManager.findFirstVisibleItemPosition();
//
//                    if (!loading && (visibleItemCount + pastVisiblesItems) >= totalItemCount) {
//                        loading = true;
//                        page++;
////                        LoadingDialogUtils.showProgress(getContext(), "正在加载,请稍后~");
//                        model.getData(page, id, DianzhuCustomActivity.this);
//                    }
//                } else {
//                    loading = false;
//                }
//            }
//        });
        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(layoutManager) {
            @Override
            public void onLoadMore() {
                page++;
                LoadingDialogUtils.showProgress(DianzhuCustomActivity.this, "正在加载,请稍后~");
                model.getData(page, id, DianzhuCustomActivity.this);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_order:
                Intent intent = new Intent(this, CustomOrderActivity.class);
                intent.putExtra("id", id);
                Log.d("dadada", id);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
        model.getData(page, id, this);
    }

    /**
     * @param object 获取客户信息成功
     */
    @Override
    public void ongetDataSuccess(Object object) {
        if (srllistview.isRefreshing()) {
            srllistview.setRefreshing(false);
        }
        Recommend_Data redata = (Recommend_Data) object;

        if (page == 1) {
            data.clear();
          //  tvnum.setText(redata.getCountRecommend()+"");
        } else {
            if ((redata.getRecommendInfo()).size() == 0) {
                return;
            }
        }
        data.addAll(redata.getRecommendInfo());
     //  tvnum.setText(mNum);
        if (data.size() == 0) {
            rlnodata.setVisibility(View.VISIBLE);
        } else {
            rlnodata.setVisibility(View.GONE);
        }
        if (adapter == null) {
            adapter = new CommonHeadAdapter<recommendInfo_data>(this, R.layout.item_recommend, data) {
                @Override
                public void convert(ViewHolder holder, recommendInfo_data recommendInfo_data, int position) {
                    if (ASUtils.isCanUse(recommendInfo_data.getAvatar())) {
                        holder.setImageURI(R.id.civ_image, recommendInfo_data.getAvatar());
                    }
                    holder.setText(R.id.tv_name, recommendInfo_data.getNickname());
                    if (ASUtils.isCanUse(recommendInfo_data.getOrderSale())) {
                        holder.setText(R.id.tv_money, NumberUtils.DecimalFormat(recommendInfo_data.getOrderSale()));
                    }
                    holder.setText(R.id.tv_num, recommendInfo_data.getCountrecommend());
                   // String mNum = recommendInfo_data.getCountrecommend();
                    holder.getImageView(R.id.iv_more).setVisibility(View.GONE);
                }


                @Override
                public void headconvert(HeaderViewHolder holder) {

                }
            };
            recyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
    }

    /**
     * @param object 获取客户信息失败
     */
    @Override
    public void ongetDataError(Object object) {
        if (srllistview.isRefreshing()) {
            srllistview.setRefreshing(false);
        }
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }
}
